﻿Public Class frmLogin
    Private frmUserList As List(Of ClsUser)
    Private frmLedgerList As List(Of ClsLedger)
    'Private appInfoConnectionString As String

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Environment.Exit(0)
    End Sub

    Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '设置窗体的text
        'Me.Text = My.Application.Info.Description

        '禁止用户直接输入内容，只能选择下拉
        Me.cmbUserName.DropDownStyle = ComboBoxStyle.DropDownList
        Me.cmbLedger.DropDownStyle = ComboBoxStyle.DropDownList
        '检测程序信息
        sysAppCurrentPath = Environment.CurrentDirectory '保存全局当前运行目录
        Dim appDataPath As String = System.IO.Path.Combine(sysAppCurrentPath, APP_DATA_PATH) '"data")
        Dim tmpDataPath As String = System.IO.Path.Combine(appDataPath, APP_DATA_NAME_SYS) '"sys.mdb")
        '程序信息数据库连接字串
        'sysAppInfoConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & tmpDataPath & ";Persist Security Info=True"
        sysAppInfoConnectionString = String.Format(sysConnectionTemplate, tmpDataPath)
        '用户数据模板数据库连接字串
        'sysDataTemplateConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & IO.Path.Combine(appDataPath, APP_DATA_NAME_TEMPLATE) & ";Persist Security Info=True"
        sysDataTemplateConnectionString = String.Format(sysConnectionTemplate, IO.Path.Combine(appDataPath, APP_DATA_NAME_TEMPLATE))

        '读取程序信息
        Dim tmpClsDbAccess As ClsAppDbAccess = New ClsAppDbAccess(sysAppInfoConnectionString, TABLE_NAME_APP)
        '放到全局对象中
        modGlobal.sysClsApp = tmpClsDbAccess.GetAppInfo()

        '读取当前用户信息
        Dim tmpUserAccess As ClsUserDbAccess = New ClsUserDbAccess(sysAppInfoConnectionString, TABLE_NAME_USER)
        frmUserList = tmpUserAccess.GetAllUser()

        '将用户信息填写到对应的comboBox列表中
        For Each tmpUser In frmUserList
            Me.cmbUserName.Items.Add(tmpUser.UserName)
        Next

    End Sub

    Private Sub cmbUserName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbUserName.SelectedIndexChanged
        Dim tmpComboBox As ComboBox = CType(sender, ComboBox)
        Dim selectedUserName As String = CType(tmpComboBox.SelectedItem, String)
        Dim resultIndex As Integer = -1

        resultIndex = tmpComboBox.FindStringExact(tmpComboBox.SelectedItem)
        '全局用户对象
        sysCurrentClsUser = frmUserList.Item(resultIndex)

        '取出该用户的可管理的全部账套
        Dim tmpClsUserLedgerDB As ClsUserLedgerDbAccess = New ClsUserLedgerDbAccess(sysAppInfoConnectionString, TABLE_NAME_USERLEDGER)
        Dim tmpList1 As List(Of ClsUserLedger) = tmpClsUserLedgerDB.GetUserLedgerByUserId(sysCurrentClsUser.UserId)

        '判断当前用户是否有可以管理的账套
        If tmpList1.Count > 0 Then
            Dim tmpSqlCondition As String
            '
            tmpSqlCondition = " [LedgerId] in("
            For Each tmpL As ClsUserLedger In tmpList1
                tmpSqlCondition &= tmpL.LedgerId.ToString
                tmpSqlCondition &= ","
            Next
            tmpSqlCondition = tmpSqlCondition.Substring(0, tmpSqlCondition.LastIndexOf(","))

            tmpSqlCondition &= ")"
            Dim tmpClsLedgerDB As ClsLedgerDbAccess = New ClsLedgerDbAccess(sysAppInfoConnectionString, TABLE_NAME_LEDGER)
            frmLedgerList = tmpClsLedgerDB.GetLedgerByCondition(tmpSqlCondition)

            '将账套加入到账套列表
            '先清除列表
            Me.cmbLedger.Items.Clear()

            If frmLedgerList.Count > 0 Then

                For Each tmpC As ClsLedger In frmLedgerList
                    Me.cmbLedger.Items.Add(tmpC.LedgerName)
                Next

                Me.cmbLedger.SelectedIndex = 0

            End If
        Else
            '当前用户没有可以管理的账套
            '判断当前用户是否有管理权限 
            If Not sysCurrentClsUser.UserIfAdmin Then
                '提醒选择一个管理员权限账号
                MessageBox.Show("当前选择的用户没有可管理的账套，也没有管理账套的权限。" & vbCrLf & "请选择一个有账套的用户或者一个有账套管理权限的用户。", "锐剑账务管理系统 - 登录")
            End If
        End If


    End Sub

    Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
        '判断是否选择了用户名以及输入了用户密码
        If Me.cmbUserName.SelectedIndex = -1 Then
            MessageBox.Show("请选择用户。", "锐剑财务管理系统 - 用户登录")
            Exit Sub
        End If
        '判断是否选择了相应的账套
        If Me.cmbLedger.SelectedIndex = -1 And (Not sysCurrentClsUser.UserIfAdmin) Then
            MessageBox.Show("请选择要打开的账套或者选择一个有账套管理权限的用户登录。", "锐剑财务管理系统 - 用户登录")
            Exit Sub
        ElseIf Me.cmbLedger.SelectedIndex <> -1 Then
            sysCurrentClsLedger = frmLedgerList.Item(Me.cmbLedger.SelectedIndex)
        End If

        '判断是否输入了密码
        If String.IsNullOrEmpty(Me.txtUserPassword.Text) Then
            MessageBox.Show("请输入用户密码。", "锐剑财务管理系统 - 用户登录")
            Exit Sub
        End If
        '查询输入的密码是否正确
        '当下全部用户列表已存在窗体集合变量中
        Dim inputStr As String = Md5Encrypt(Me.txtUserPassword.Text)

        If String.Compare(inputStr, sysCurrentClsUser.UserPassword) = 0 Then

            Try
                '判断是否选择了账套
                If sysCurrentClsLedger IsNot Nothing Then

                    '判断是否注册
                    If Not sysClsApp.AppRegisted Then
                        sysCurrentClsLedger.LedgerCompanyName = "未注册用户"
                    End If
                    '设置全局当前选定账套的数据库连接字串
                    'sysUserDataConnectionString 
                    Dim tmpPath As String = IO.Path.Combine(IO.Path.Combine(sysAppCurrentPath, APP_DATA_PATH), sysCurrentClsLedger.LedgerDataRealPath)
                    'sysUserDataConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & tmpPath & ";Persist Security Info=True"
                    sysUserDataConnectionString = String.Format(sysConnectionTemplate, tmpPath)

                    '设定关闭参数
                    Me.DialogResult = DialogResult.OK
                Else
                    '如果没有选择账套，即是管理员单独登录
                    '设定关闭参数，返回NO值以通知主窗体，没有选择账套
                    Me.DialogResult = DialogResult.No

                End If


                '关闭自身
                Me.Close()

            Catch ex As Exception
                mErrorNumber = -1002
                mErrorMessage = ex.Message
                mErrorCreater = "frmLogin - btnEnter_click"
            End Try
        Else
            '提示密码错误
            MessageBox.Show("密码错误。密码区分大小写。", "登录提示")
            Me.txtUserPassword.Select()
        End If
    End Sub

    Private Sub cmbUserName_KeyUp(sender As Object, e As KeyEventArgs) Handles cmbUserName.KeyUp
        If e.KeyCode = Keys.Down And (Not Me.cmbUserName.DroppedDown) Then
            '下拉列表
            Me.cmbUserName.DroppedDown = True
        End If

        If e.KeyCode = Keys.Enter And Me.cmbUserName.SelectedIndex <> -1 Then
            '跳到下一个控件
            Me.cmbLedger.Select()
        End If
    End Sub

    Private Sub cmbLedger_KeyUp(sender As Object, e As KeyEventArgs) Handles cmbLedger.KeyUp
        If e.KeyCode = Keys.Down And (Not Me.cmbLedger.DroppedDown) Then
            '下拉列表
            Me.cmbLedger.DroppedDown = True
        End If

        If e.KeyCode = Keys.Enter And Me.cmbLedger.SelectedIndex <> -1 Then
            '跳到下一个控件
            Me.txtUserPassword.Select()
        End If
    End Sub

    Private Sub txtUserPassword_KeyUp(sender As Object, e As KeyEventArgs) Handles txtUserPassword.KeyUp
        If e.KeyCode = Keys.Enter AndAlso Me.txtUserPassword.Text.Length > 0 Then
            '跳到下一个控件
            Me.btnEnter.Select()
        End If
    End Sub

End Class
